const { createClient } = require("redis");
const WinexAuthorizationGenerator = require("./WinexAuthorizationGenerator");
const WinexInpatientBaseGenerator = require("./inpatient/WinexInpatientBaseGenerator");
const UserGenerator = require("./UserGenerator");
const WinexScheduleGenerator = require("./WinexScheduleGenerator");

class WinexDataGeneration {

    constructor(redisHost = "127.0.0.1", redisPort = 6379, redisDb = 0) {
        this.redisHost = redisHost;
        this.redisPort = redisPort;
        this.redisDb = redisDb;
        this.winexAuthorizationGenerator = new WinexAuthorizationGenerator(this.redisHost, this.redisPort, this.redisDb);
        this.winexInpatientBaseGenerator = new WinexInpatientBaseGenerator(this.redisHost, this.redisPort, this.redisDb);
        this.userGenerator = new UserGenerator(this.redisHost, this.redisPort, this.redisDb);
        this.winexScheduleGenerator = new WinexScheduleGenerator(this.redisHost, this.redisPort, this.redisDb);
    }
    async generate(loginUrl, adminLoginData, hisLoginData, outpatientDoctorLoginData, inpatientDoctorLoginData, inpatientNurseLoginData) {
        await Promise.all([
            this.userGenerator.generate(100000),
            adminLoginData && this.winexAuthorizationGenerator.generate(loginUrl, adminLoginData, "adminAuthorizations", 50),
            hisLoginData && this.winexAuthorizationGenerator.generate(loginUrl, hisLoginData, "hisAuthorizations", 50),
            outpatientDoctorLoginData && this.winexAuthorizationGenerator.generate(loginUrl, outpatientDoctorLoginData, "outpatientDoctorAuthorizations", 50),
            inpatientDoctorLoginData && this.winexAuthorizationGenerator.generate(loginUrl, inpatientDoctorLoginData, "inpatientDoctorAuthorizations", 50),
            inpatientNurseLoginData && this.winexAuthorizationGenerator.generate(loginUrl, inpatientNurseLoginData, "inpatientNurseAuthorizations", 50),
        ]);
        if (hisLoginData) {
            await this.winexScheduleGenerator.generate();
        }
        if (adminLoginData) {
            await this.winexInpatientBaseGenerator.generate("性能", 20, 10, 5);
        }
    }

    async clean() {
        const client = await createClient({ host: this.redisHost, port: this.redisPort }).on("error", () => { }).connect();
        await client.select(this.redisDb);
        await client.del("adminAuthorizations");
        await client.del("hisAuthorizations");
        await client.del("outpatientDoctorAuthorizations");
        await client.del("inpatientDoctorAuthorizations");
        await client.del("inpatientNurseAuthorizations");
        await client.del("users");
        await client.del("schedules");
        await client.del("inpatientBeds");
        await client.disconnect();
        await this.winexInpatientBaseGenerator.clean("性能");
    }

}

module.exports = WinexDataGeneration;